<?php

/*
 * Fresns Control Panel (Standalone)
 * Copyright (C) 2021-Present 刘鑫
 * Released under the MIT License.
 */

namespace App\Plugins\FcpApi\TweetAdmin;

use App\Base\Config\BaseConfig;
use App\Helpers\CommonHelper;
use App\Http\Center\Common\ErrorCodeService;
use App\Http\Center\Common\ValidateService;
use App\Plugins\FcpApi\Base\TweetBaseController;
use App\Plugins\FcpApi\TweetUsers\TweetUsers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;

class FsControllerAdmin extends TweetBaseController
{
    public function __construct()
    {
        $this->checkRequest();

        $this->service = new FsService();
    }

    public function index(Request $request)
    {
        $request->offsetSet('user_type', FsConfig::USER_TYPE_ADMIN);

        parent::index($request);
    }

    //查询管理员用户
    public function searchUser(Request $request)
    {
        $keyword = $request->input('keyword');
        $data['list'] = [];
        if ($keyword) {
            $userArr1 = TweetUsers::where('user_type', FsConfig::USER_TYPE_USER)->where('phone', 'LIKE', "%$keyword%")->pluck('id')->toArray();
            $userArr2 = TweetUsers::where('user_type', FsConfig::USER_TYPE_USER)->where('email', 'LIKE', "%$keyword%")->pluck('id')->toArray();
            $userIdArr = array_merge($userArr1, $userArr2);

            $data['list'] = TweetUsers::whereIn('id', $userIdArr)->get(['id', 'phone', 'email'])->toArray();
        }
        $this->success($data);
    }

    //新建管理员
    public function storeAdmin(Request $request)
    {
        ValidateService::validateRule($request, $this->rules(BaseConfig::RULE_UPDATE));

        $this->hookUpdateValidateAfter();

        $id = $request->input('id');
        $input = [
            'user_type' => FsConfig::USER_TYPE_ADMIN,
        ];

        TweetUsers::where('id', $id)->update($input);

        $this->success();
    }

    //删除管理员
    public function destroyAdmin(Request $request)
    {
        ValidateService::validateRule($request, $this->rules(BaseConfig::RULE_UPDATE));

        $this->hookUpdateValidateAfter();

        $id = $request->input('id');
        $input = [
            'user_type' => FsConfig::USER_TYPE_USER,
        ];

        TweetUsers::where('id', $id)->update($input);

        $this->success();
    }

    // 重置密码
    public function resetPassword(Request $request)
    {
        ValidateService::validateRule($request, $this->rules(FsConfig::RESET_PASSWORD));

        // 更新密码
        $id = $request->input('id');
        $newPassword = $request->input('newPassword');
        $this->service->updatePassword($id, $newPassword);

        CommonHelper::removeRequestFields($this->service->getSearchableFields());

        // // 返回详情
        // $data = $this->service->detail($id);

        parent::index($request);
    }

    // 更新密码
    public function updatePassword(Request $request)
    {
        ValidateService::validateRule($request, $this->rules(FsConfig::UPDATE_PASSWORD));

        // 验证原始密码
        $user = Auth::user();

        $validPassword = $user->getAuthPassword();
        $oldPassword = $request->input('oldPassword');
        if (! password_verify($oldPassword, $validPassword)) {
            $this->error(ErrorCodeService::ACCOUNT_PASSWORD_INVALID);
        }

        // 更新密码
        $id = $user->id;
        $newPassword = $request->input('newPassword');
        $this->service->updatePassword($id, $newPassword);

        CommonHelper::removeRequestFields($this->service->getSearchableFields());

        // // 返回详情
        // $data = $this->service->detail($id);

        parent::index($request);
    }

    // 验证规则
    public function rules($ruleType)
    {
        $rule = [];

        $FsConfig = new FsConfig($this->service->getTable());

        switch ($ruleType) {
            case BaseConfig::RULE_STORE:
                $rule = $FsConfig->storeRule();
                break;

            case BaseConfig::RULE_UPDATE:
                $rule = $FsConfig->updateRule();
                break;

            case BaseConfig::RULE_DESTROY:
                $rule = $FsConfig->destroyRule();
                break;

            case BaseConfig::RULE_DETAIL:
                $rule = $FsConfig->detailRule();
                break;

            case FsConfig::RESET_PASSWORD:
                $rule = $FsConfig->resetPasswordRule();
                break;

            case FsConfig::UPDATE_PASSWORD:
                $rule = $FsConfig->updatePasswordRule();
                break;
            case BaseConfig::RULE_STORE_MORE_JSON:
                $rule = $FsConfig->storeMoreJsonRule();
                break;
            case BaseConfig::RULE_UPDATE_MORE_JSON:
                $rule = $FsConfig->updateMoreJsonRule();
                break;
        }

        return $rule;
    }
}
